﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace QuantumQuest
{
    public partial class QQRocket : QQBody
    {
        private double _fuel;
        private double _thrustIncrement;
        private double _rotateIncrement;

        private QQVector2 _thrustVec;
        private double _thrustDeg;
        
        private DateTime _thrustDelay = DateTime.Now;
        private DateTime _rotateDelay = DateTime.Now;


        public QQRocket(double fuel, double mass, QQVector2 position):base(position, new QQVector2(0, 0), new QQVector2(0, 0), mass, 0, false, false)
        {
            //_zIndex = 99;
            InitializeComponent();
            

           
            _thrustVec = new QQVector2(0, 0);
            _thrustDeg = 0.0;
            //_thrustVec.ThetaRad = Math.PI / 2.0;
            _position = position;
            _mass = mass;
            _fuel = fuel;
            _collisionRadius = 5;
            _rotateIncrement = 6;
            _thrustIncrement = _fuel / 20.0;
            UpdateBody();
            
            
          

            
        }

       
        public void RotateView(double deg)
        {
            _thrustDeg -= deg;
            _rotate.Angle-=deg;
            //RocketRotate.Angle -= deg;
            //_thrustVec.ThetaDeg += deg;
            _thrustVec.ThetaDeg =_thrustDeg;
            //UpdateBody();
        }
        public override void Update()
        {
            
            //_thrustVec.Magnitude = 0.0;

            if(QQGlobal.KeysDown.Contains(Key.Up))
            {
                IncreaseThrust(_thrustIncrement);
                
            }
            if(QQGlobal.KeysDown.Contains(Key.Down))
            {
                DecreaseThrust(_thrustIncrement);
            }
            if(QQGlobal.KeysDown.Contains(Key.Left))
            {
                RotateDelayed(_rotateIncrement);
            }
            if(QQGlobal.KeysDown.Contains(Key.Right))
            {
                RotateDelayed(-1.0 * _rotateIncrement);
            }



            if (_fuel > 0.0)
            {
                
                _velocity += _thrustVec;
                _fuel -= (_thrustVec.Magnitude * (QQGlobal.Dt));
            }
            else
            {
                _thrustVec.Magnitude = 0.0;
                _fuel = 0.0;
            }
            //UpdateBody();
        }
        public bool IncreaseThrust(double amount)
        {
            if ((DateTime.Now-_thrustDelay).Milliseconds >= 200)
            {
                 _thrustDelay = DateTime.Now;
                if (_thrustVec.Magnitude < _fuel)
                {

                    _thrustVec.Magnitude += amount;
                    RotateView(0);
                   
                }
               
                return true;
            }
            return false;
        }
        public void DecreaseThrust(double amount)
        {
            if (_thrustVec.Magnitude > 0.0)
            {
                _thrustVec.Magnitude -= amount;
            }
            if (_thrustVec.Magnitude <= 0.001)
            {
                _thrustVec.Magnitude = 0.0;
            }

        }
        public bool RotateDelayed(double deg) 
        {
            if ((DateTime.Now - _rotateDelay).Milliseconds >= 20)
            {
                RotateView(deg);
                _rotateDelay = DateTime.Now;
                
                return true;
            }
            return false;
        }
        public QQVector2 ThrustVec
        {
            get
            {
                return _thrustVec;
            }
            set
            {
                _thrustVec = value;
            }
        }
        public double Fuel
        {
            get
            {
                return _fuel;
            }
            set
            {
                _fuel = value;
            }
        }
        public double ThrustIncrement
        {
            get
            {
                return _thrustIncrement;
            }
            set
            {
                _thrustIncrement = value;
            }
        }

       
        
    }
}
